package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
@Slf4j
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper paramMapper;

    /**
     * 查询规格组
     * @param id
     * @return
     */
    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        // 查询规格组
        SpecGroup s = new SpecGroup();
        s.setCid(id);
        List<SpecGroup> list = groupMapper.select(s);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        // 对象转换
        return BeanHelper.copyWithCollection(list, SpecGroupDTO.class);
    }


    /**
     * 查询规格组查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParamDTO> querySpecParams(Long gid) {
        SpecParam s = new SpecParam();
        s.setGroupId(gid);
        List<SpecParam> list = paramMapper.select(s);
        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, SpecParamDTO.class);
    }

    /**
     * 添加规格组
     * @param specGroupDTO
     */
    public void saveSpecGroup(SpecGroupDTO specGroupDTO) {

        SpecGroup specGroup = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        int i = groupMapper.insertSelective(specGroup);
        if (i==0) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

    }

    /**
     * 根据组名删除改组和该组的规格
     * @param gid
     */
    @Transactional
    public void deleteSpecParams(Long gid) {

        //删除组下面的规格参数
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        try {
            groupMapper.deleteByPrimaryKey(gid);
            //删除组
            paramMapper.delete(specParam);
        } catch (Exception e) {
           log.info("删除规格组或者规格参数有误！");
           throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
    }

    /**
     * 添加规格参数
     * @param specParamDTO
     */
    public void saveSpecParams(SpecParamDTO specParamDTO) {
        SpecParam specParam = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int i = paramMapper.insertSelective(specParam);
        if (i==0) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
    }
}